package com.ctshk.rpc.certificate.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.certificate.dto.*;
import com.ctshk.rpc.certificate.entity.CertificateBusiness;
import com.ctshk.rpc.certificate.entity.CertificateBusinessBookingTime;
import com.ctshk.rpc.certificate.entity.CertificateBusinessBookingTimeDetails;
import com.ctshk.rpc.certificate.mapper.CertificateBusinessBookingTimeDetailsMapper;
import com.ctshk.rpc.certificate.mapper.CertificateBusinessBookingTimeMapper;
import com.ctshk.rpc.certificate.mapper.CertificateBusinessMapper;
import com.ctshk.rpc.certificate.mapper.es.CertificateMapper;
import com.ctshk.rpc.certificate.req.*;
import com.ctshk.rpc.certificate.service.ICertificateBusinessService;
import com.ctshk.rpc.system.dto.MainDataBusinessDTO;
import com.ctshk.rpc.system.req.MainDataBusinessListReq;
import com.ctshk.rpc.system.service.IMainDataBusinessService;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * <p>
 * 证件业务 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-19
 */
@DubboService
public class CertificateBusinessServiceImpl extends ServiceImpl<CertificateBusinessMapper, CertificateBusiness> implements ICertificateBusinessService {

    @Autowired
    private CertificateBusinessMapper certificateBusinessMapper;
    @Autowired
    private CertificateBusinessBookingTimeDetailsMapper detailsMapper;
    @Autowired
    private CertificateBusinessBookingTimeMapper timeMapper;
    @DubboReference
    private IMainDataBusinessService mainDataBusinessService;
    @Autowired
    private CertificateMapper certificateMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result saveCertificate(CertificateReq certificateReq, TokenUser user) {
        if (user == null) {
            return Result.failed(SystemError.USER_1000);
        }
        if (certificateReq == null) {
            return Result.failed(SystemError.SYS_416);
        }

        // 查询证件名称是否重复， 重复不添加
        Integer count = certificateBusinessMapper.selectCount(new QueryWrapper<CertificateBusiness>().lambda()
                .eq(CertificateBusiness::getName, certificateReq.getName()));
        if (count != 0) {
            return Result.failed(SystemError.CERTIFICATE_BUSINESS_16000);
        }

        // 判断可预约时间不能有交集
        List<LocalDate> startDateList = new ArrayList<>();
        List<LocalDate> endDateList = new ArrayList<>();
        if(null != certificateReq.getBusinessHallAndTimeList()){
            certificateReq.getBusinessHallAndTimeList().forEach(businessHallAndTimeReq -> {
                startDateList.add(businessHallAndTimeReq.getStartDate());
                endDateList.add(businessHallAndTimeReq.getEndDate());
            });
        }
        if (isRetain(startDateList, endDateList)) {
            return Result.failed(SystemError.CERTIFICATE_BUSINESS_16001);
        }

        // 保存证件
        CertificateBusiness certificateBusiness = new CertificateBusiness();
        BeanUtils.copyProperties(certificateReq, certificateBusiness);
        if (CollectionUtils.isNotEmpty(certificateReq.getHandlingInstructionsList())) {
            String handlingInstructions = JSON.toJSONString(certificateReq.getHandlingInstructionsList());
            certificateBusiness.setHandlingInstructions(handlingInstructions);
        }
        Long businessId = SnowflakeIdWorker.nextId();
        certificateBusiness.setId(businessId);
        certificateBusiness.setCreateId(user.getId());
        certificateBusiness.setGmtCreate(LocalDateTime.now());
        certificateBusiness.setStatus(1);
        certificateBusiness.setIsDeleted(0);
        certificateBusinessMapper.insert(certificateBusiness);

        // 判断类型 类型 1 办理须知（没有可預約辦證中心及時段信息）， 2预约登记、办理须知
//        List<CertificateBusinessBookingTime> businessBookingTimeList = new ArrayList<>();
//        if (certificateReq.getBusinessTypeId().equals(CertificateBusinessType.CERTIFICATE_BOOKING.getCode())) {
//            certificateReq.getBusinessHallAndTimeList().forEach(businessHallAndTimeReq -> {
//                // 保存可预约时间信息
//                CertificateBusinessBookingTime businessBookingTime = new CertificateBusinessBookingTime();
//                Long businessBookingTimeId = SnowflakeIdWorker.nextId();
//                businessBookingTime.setId(businessBookingTimeId);
//                businessBookingTime.setBusinessId(businessId);
//                businessBookingTime.setStartDate(businessHallAndTimeReq.getStartDate());
//                businessBookingTime.setEndDate(businessHallAndTimeReq.getEndDate());
//                businessBookingTime.setCreateId(user.getId());
//                businessBookingTime.setGmtCreate(LocalDateTime.now());
//                businessBookingTime.setStatus(1);
//                businessBookingTime.setIsDeleted(0);
//                businessBookingTimeList.add(businessBookingTime);
//
//                // 保存办证中心信息
//                businessHallAndTimeReq.getBusinessReqList().forEach(businessReq -> {
//                    List<CertificateBusinessBookingTimeDetails> detailsList = new ArrayList<>();
//                    businessReq.getBusinessHallReqList().forEach(businessHall -> {
//                        CertificateBusinessBookingTimeDetails bookingTimeDetails = new CertificateBusinessBookingTimeDetails();
//                        Long bookingTimeDetailsId = SnowflakeIdWorker.nextId();
//                        bookingTimeDetails.setId(bookingTimeDetailsId);
//                        bookingTimeDetails.setBusinessId(businessId);
//                        bookingTimeDetails.setTimeId(businessBookingTimeId);
//                        BeanUtils.copyProperties(businessHall, bookingTimeDetails);
//                        bookingTimeDetails.setCreateId(user.getId());
//                        bookingTimeDetails.setGmtCreate(LocalDateTime.now());
//                        bookingTimeDetails.setStatus(1);
//                        bookingTimeDetails.setIsDeleted(0);
//                        bookingTimeDetails.setBusinessHallId(businessReq.getBusinessHallId());
//                        bookingTimeDetails.setBusinessHallName(businessReq.getBusinessHallName());
//                        bookingTimeDetails.setRemainNumber(businessHall.getNumber());
//                        detailsList.add(bookingTimeDetails);
//                    });
//                    detailsMapper.insertBatchSomeColumn(detailsList);
//                });
//            });
//            timeMapper.insertBatchSomeColumn(businessBookingTimeList);
//        }

        // 存储证件服务中心到es
        CertificatePageReq certificatePageReq = new CertificatePageReq();
        certificatePageReq.setPageSize(Integer.MAX_VALUE);
        PageResponse<CertificateDTO> page = queryList(certificatePageReq);
        certificateMapper.saveAll(page.getRecords());
        return Result.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result updateCertificate(CertificateReq certificateReq, TokenUser user) {
        if (user == null) {
            return Result.failed(SystemError.USER_1000);
        }
        if (certificateReq == null) {
            return Result.failed(SystemError.SYS_416);
        }

        // 判断可预约时间不能有交集
        List<LocalDate> startDateList = new ArrayList<>();
        List<LocalDate> endDateList = new ArrayList<>();
        if (null != certificateReq.getBusinessHallAndTimeList()) {
            certificateReq.getBusinessHallAndTimeList().forEach(businessHallAndTimeReq -> {
                startDateList.add(businessHallAndTimeReq.getStartDate());
                endDateList.add(businessHallAndTimeReq.getEndDate());
            });
        }
        if (isRetain(startDateList, endDateList)) {
            return Result.failed(SystemError.CERTIFICATE_BUSINESS_16001);
        }
        Long businessId = certificateReq.getId();
        CertificateBusiness certificateBusiness = certificateBusinessMapper.selectById(certificateReq.getId());
        if (Objects.isNull(certificateBusiness)) {
            return Result.failed(SystemError.SYS_411);
        }
        BeanUtils.copyProperties(certificateReq, certificateBusiness);
        if (CollectionUtils.isNotEmpty(certificateReq.getHandlingInstructionsList())) {
            String handlingInstructions = JSON.toJSONString(certificateReq.getHandlingInstructionsList());
            certificateBusiness.setHandlingInstructions(handlingInstructions);
        }
        certificateBusiness.setModifiedId(user.getId());
        certificateBusiness.setGmtModified(LocalDateTime.now());
        certificateBusinessMapper.updateById(certificateBusiness);

        // 判断类型 类型 1 办理须知（没有可預約辦證中心及時段信息）， 2预约等级
//        if (certificateReq.getBusinessTypeId().equals(CertificateBusinessType.CERTIFICATE_BOOKING.getCode())) {
//            // 删除旧的保存可预约时间信息
//            List<CertificateBusinessBookingTime> timeList = timeMapper.selectList(Wrappers.<CertificateBusinessBookingTime>lambdaQuery()
//                    .eq(CertificateBusinessBookingTime::getBusinessId, businessId));
//            List<Long> timeIdList = timeList.stream().map(CertificateBusinessBookingTime::getId).collect(Collectors.toList());
//            timeMapper.delete(new QueryWrapper<CertificateBusinessBookingTime>().lambda().eq(CertificateBusinessBookingTime::getBusinessId, businessId));
//
//            // 删除办证中心信息
//            QueryWrapper<CertificateBusinessBookingTimeDetails> oldQueryWrapper = new QueryWrapper<>();
//            oldQueryWrapper.lambda().eq(CertificateBusinessBookingTimeDetails::getBusinessId, businessId);
//            if (CollectionUtils.isNotEmpty(timeIdList)) {
//                oldQueryWrapper.lambda().in(CertificateBusinessBookingTimeDetails::getTimeId, timeIdList);
//            }
//            detailsMapper.delete(oldQueryWrapper);
//
//            List<CertificateBusinessBookingTime> businessBookingTimeList = new ArrayList<>();
//            certificateReq.getBusinessHallAndTimeList().forEach(businessHallAndTimeReq -> {
//                // 保存可预约时间信息
//                CertificateBusinessBookingTime businessBookingTime = new CertificateBusinessBookingTime();
//                Long businessBookingTimeId = SnowflakeIdWorker.nextId();
//                businessBookingTime.setId(businessBookingTimeId);
//                businessBookingTime.setBusinessId(businessId);
//                businessBookingTime.setStartDate(businessHallAndTimeReq.getStartDate());
//                businessBookingTime.setEndDate(businessHallAndTimeReq.getEndDate());
//                businessBookingTime.setCreateId(user.getId());
//                businessBookingTime.setGmtCreate(LocalDateTime.now());
//                businessBookingTime.setStatus(1);
//                businessBookingTime.setIsDeleted(0);
//                businessBookingTimeList.add(businessBookingTime);
//
//                // 保存办证中心信息
//                businessHallAndTimeReq.getBusinessReqList().forEach(businessReq -> {
//                    List<CertificateBusinessBookingTimeDetails> detailsList = new ArrayList<>();
//                    businessReq.getBusinessHallReqList().forEach(businessHall -> {
//                        CertificateBusinessBookingTimeDetails bookingTimeDetails = new CertificateBusinessBookingTimeDetails();
//                        Long bookingTimeDetailsId = SnowflakeIdWorker.nextId();
//                        bookingTimeDetails.setId(bookingTimeDetailsId);
//                        bookingTimeDetails.setBusinessId(businessId);
//                        bookingTimeDetails.setTimeId(businessBookingTimeId);
//                        BeanUtils.copyProperties(businessHall, bookingTimeDetails);
//                        bookingTimeDetails.setCreateId(user.getId());
//                        bookingTimeDetails.setGmtCreate(LocalDateTime.now());
//                        bookingTimeDetails.setStatus(1);
//                        bookingTimeDetails.setIsDeleted(0);
//                        bookingTimeDetails.setBusinessHallId(businessReq.getBusinessHallId());
//                        bookingTimeDetails.setBusinessHallName(businessReq.getBusinessHallName());
//                        bookingTimeDetails.setRemainNumber(businessHall.getNumber());
//                        detailsList.add(bookingTimeDetails);
//                    });
//                    detailsMapper.insertBatchSomeColumn(detailsList);
//                });
//            });
//            timeMapper.insertBatchSomeColumn(businessBookingTimeList);
//        }

        // 存储证件服务中心到es
        CertificatePageReq certificatePageReq = new CertificatePageReq();
        certificatePageReq.setPageSize(Integer.MAX_VALUE);
        PageResponse<CertificateDTO> page = queryList(certificatePageReq);
        certificateMapper.saveAll(page.getRecords());
        return Result.success();
    }

    @Override
    public PageResponse<CertificateDTO> queryList(CertificatePageReq req) {
        //查询es对象
        if (null != req.getBusinessTypeId()) {
            MatchQueryBuilder matchQueryBuilder = null;
            MatchQueryBuilder name = null;
            BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
            if (null != req.getBusinessTypeId()){
                matchQueryBuilder = QueryBuilders.matchQuery("businessTypeId", req.getBusinessTypeId());
                boolQueryBuilder.must(matchQueryBuilder);
            }
            if (null != req.getName()){
                name = QueryBuilders.matchQuery("name", req.getName());
                boolQueryBuilder.must(name);
            }
            Iterable<CertificateDTO> iterable = certificateMapper.search(boolQueryBuilder);
            List<CertificateDTO> list = Lists.newArrayList(iterable);
            if (CollectionUtils.isNotEmpty(list)) {
                return new PageResponse<CertificateDTO>(page(list, req.getPageSize(), req.getPageNo()),
                        req.getPageNo() == 1, list.size() <= (req.getPageNo() * req.getPageSize()),
                        list.size(), req.getPageNo(), req.getPageSize());
            }
        }

        Page<CertificateBusiness> certificatePage = new Page<>(req.getPageNo(), req.getPageSize());
        QueryWrapper<CertificateBusiness> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(req.getBusinessTypeId() != null, CertificateBusiness::getBusinessTypeId, req.getBusinessTypeId());
        queryWrapper.lambda().eq(CertificateBusiness::getStatus, 1);
        queryWrapper.lambda().eq(CertificateBusiness::getIsDeleted, 0);
        queryWrapper.lambda().like(req.getName() != null, CertificateBusiness::getName, req.getName());
        queryWrapper.lambda().orderByDesc(CertificateBusiness::getGmtCreate);
        Page<CertificateBusiness> iPage = certificateBusinessMapper.selectPage(certificatePage, queryWrapper);
        List<CertificateBusiness> records = iPage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return new PageResponse<>(new ArrayList<>(), iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
        }
        List<CertificateDTO> certificateQueryDTOList = EntityUtil.copyList(records, CertificateDTO.class);
        return new PageResponse<>(certificateQueryDTOList, iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
    }

    /**
     * 循环截取某页列表进行分页
     *
     * @param dataList    分页数据
     * @param pageSize    页面大小
     * @param currentPage 当前页面
     */
    private List page(List dataList, int pageSize, int currentPage) {
        List currentPageList = new ArrayList<>();
        if (dataList != null && dataList.size() > 0) {
            int currIdx = (currentPage > 1 ? (currentPage - 1) * pageSize : 0);
            for (int i = 0; i < pageSize && i < dataList.size() - currIdx; i++) {
                Object data = dataList.get(currIdx + i);
                currentPageList.add(data);
            }
        }
        return currentPageList;
    }

    @Override
    public CertificateInfoDTO queryDetail(Long id) {
        CertificateBusiness certificateBusiness = certificateBusinessMapper.selectById(id);
        CertificateInfoDTO certificateInfoDTO = new CertificateInfoDTO();
        BeanUtils.copyProperties(certificateBusiness, certificateInfoDTO);
        if (StringUtils.isNotEmpty(certificateBusiness.getHandlingInstructions())) {
            List<CertificateHandlingReq> handlingReqList = JSONObject.parseArray(certificateBusiness.getHandlingInstructions(), CertificateHandlingReq.class);
            certificateInfoDTO.setHandlingInstructionsList(handlingReqList);
        }

        // 查询可预约时间信息
        List<CertificateBusinessBookingTime> timeList = timeMapper.selectList(Wrappers.<CertificateBusinessBookingTime>lambdaQuery()
                .eq(CertificateBusinessBookingTime::getBusinessId, id));
        List<Long> timeIdList = timeList.stream().map(CertificateBusinessBookingTime::getId).collect(Collectors.toList());

        // 查询办证中心信息
        QueryWrapper<CertificateBusinessBookingTimeDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CertificateBusinessBookingTimeDetails::getBusinessId, id);
        queryWrapper.lambda().in(CollectionUtils.isNotEmpty(timeIdList), CertificateBusinessBookingTimeDetails::getTimeId, timeIdList);
        List<CertificateBusinessBookingTimeDetails> timeDetails = detailsMapper.selectList(queryWrapper);
        Map<Long, List<CertificateBusinessBookingTimeDetails>> maps = timeDetails.stream().collect(Collectors.groupingBy(CertificateBusinessBookingTimeDetails::getTimeId));

        // 组合数据
        List<BusinessHallAndTimeDTO> businessHallAndTimeList = new ArrayList<>();
        timeList.forEach(bookingTime -> {
            // 1层
            BusinessHallAndTimeDTO timeDTO = new BusinessHallAndTimeDTO();
            timeDTO.setStartDate(bookingTime.getStartDate());
            timeDTO.setEndDate(bookingTime.getEndDate());
            List<BusinessReq> businessReqList = new ArrayList<>();
            List<CertificateBusinessBookingTimeDetails> detailsList = maps.get(bookingTime.getId());
            Map<Long, List<CertificateBusinessBookingTimeDetails>> detailsMap = detailsList.stream().collect(Collectors.groupingBy(CertificateBusinessBookingTimeDetails::getBusinessHallId));
            for (Long businessHallId : detailsMap.keySet()) {
                BusinessReq businessReq = new BusinessReq();
                businessReq.setBusinessHallId(businessHallId);
                businessReq.setBusinessHallName(detailsMap.get(businessHallId).get(0).getBusinessHallName());
                List<BusinessHallReq> businessHallReqList = new ArrayList<>();
                detailsMap.get(businessHallId).forEach(details -> {
                    BusinessHallReq businessHallReq = new BusinessHallReq();
                    businessHallReq.setTimeBucket(details.getTimeBucket());
                    businessHallReq.setNumber(details.getNumber());
                    businessHallReq.setRemainNumber(details.getRemainNumber());
                    businessHallReqList.add(businessHallReq);

                });
                businessReq.setBusinessHallReqList(businessHallReqList);
                businessReqList.add(businessReq);
            }
            timeDTO.setBusinessReqList(businessReqList);
            businessHallAndTimeList.add(timeDTO);
        });
        certificateInfoDTO.setBusinessHallAndTimeList(businessHallAndTimeList);
        return certificateInfoDTO;
    }

    @Override
    public List<BusinessDTO> queryBusinessHall(Long businessId) {
        // 查询可预约时间信息
        List<CertificateBusinessBookingTime> timeList = timeMapper.selectList(Wrappers.<CertificateBusinessBookingTime>lambdaQuery()
                .eq(CertificateBusinessBookingTime::getBusinessId, businessId));
        List<Long> timeIdList = timeList.stream().map(CertificateBusinessBookingTime::getId).collect(Collectors.toList());

        // 查询办证中心信息
        QueryWrapper<CertificateBusinessBookingTimeDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CertificateBusinessBookingTimeDetails::getBusinessId, businessId);
        queryWrapper.lambda().in(CollectionUtils.isNotEmpty(timeIdList), CertificateBusinessBookingTimeDetails::getTimeId, timeIdList);
        List<CertificateBusinessBookingTimeDetails> timeDetails = detailsMapper.selectList(queryWrapper);
        Map<Long, List<CertificateBusinessBookingTimeDetails>> maps = timeDetails.stream().collect(Collectors.groupingBy(CertificateBusinessBookingTimeDetails::getTimeId));

        // 获取办证中心id 列表
        List<Long> businessHallIdList = new ArrayList<>();
        List<BusinessHallAndTimeDTO> businessHallAndTimeList = new ArrayList<>();
        timeList.forEach(bookingTime -> {
            List<CertificateBusinessBookingTimeDetails> detailsList = maps.get(bookingTime.getId());
            Map<Long, List<CertificateBusinessBookingTimeDetails>> detailsMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(detailsList)) {
                detailsMap = detailsList.stream().collect(Collectors.groupingBy(CertificateBusinessBookingTimeDetails::getBusinessHallId));
            }
            for (Long businessHallId : detailsMap.keySet()) {
                if (businessHallIdList.contains(businessHallId)) {
                    continue;
                }
                businessHallIdList.add(businessHallId);
            }
        });

        // 查询主数据办证中心信息
        MainDataBusinessListReq req = new MainDataBusinessListReq();
        req.setIdList(businessHallIdList);
        req.setPageSize(10000);
        Result<PageResponse<MainDataBusinessDTO>> result = mainDataBusinessService.list(req);
        List<BusinessDTO> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(result.getData().getRecords())) {
            list = EntityUtil.copyList(result.getData().getRecords(), BusinessDTO.class);
        }
        return list;
    }

    @Override
    public BusinessDetailDTO queryBusinessHallDesc(BusinessHallDescReq req) {
        // 根据办证中心id 查询未来一个月的数据
        LocalDate beginTime = LocalDate.now();
        LocalDate endTime = beginTime.plusMonths(1);

        List<CertificateBusinessBookingTimeDetailsDTO> timeList = timeMapper.selectWithDetailsList(req.getBusinessId(),
                beginTime, endTime, req.getBusinessHallId());

        // 按天计算可预约数量
        BusinessDetailDTO detailDTO = new BusinessDetailDTO();
        List<BusinessDescDTO> dateList = new ArrayList<>();
        while (!beginTime.isAfter(endTime)) {
            BusinessDescDTO businessDescDTO = new BusinessDescDTO();
            AtomicInteger remainNumber = new AtomicInteger(0);
            LocalDate finalBeginTime = beginTime;
            List<BusinessHallReq> businessHallList = new ArrayList<>();
            timeList.forEach(bookingTime -> {
                if ((bookingTime.getStartDate().isBefore(finalBeginTime) || bookingTime.getStartDate().isEqual(finalBeginTime))
                        && (bookingTime.getEndDate().isAfter(finalBeginTime) || bookingTime.getEndDate().isEqual(finalBeginTime))) {
                    remainNumber.addAndGet(bookingTime.getRemainNumber());
                    BusinessHallReq businessHall = new BusinessHallReq();
                    businessHall.setTimeBucket(bookingTime.getTimeBucket());
                    businessHall.setRemainNumber(bookingTime.getRemainNumber());
                    businessHallList.add(businessHall);
                }

            });
            businessDescDTO.setTime(finalBeginTime);
            businessDescDTO.setRemainNumber(remainNumber.get());
            businessDescDTO.setBusinessHallReqList(businessHallList);
            dateList.add(businessDescDTO);
            //日期+1，继续执行
            beginTime = beginTime.plusDays(1);
        }

        Result<MainDataBusinessDTO> result = mainDataBusinessService.detail(req.getBusinessHallId());
        detailDTO = EntityUtil.copy(result.getData(), BusinessDetailDTO.class);
        detailDTO.setDateList(dateList);
        return detailDTO;
    }

    private boolean isRetain(List<LocalDate> startDateList, List<LocalDate> endDateList) {
        startDateList.sort(LocalDate::compareTo);
        endDateList.sort(LocalDate::compareTo);
        for (int k = 1; k < startDateList.size(); k++) {
            if (startDateList.get(k).isBefore(endDateList.get(k - 1)) || startDateList.get(k).isEqual(endDateList.get(k - 1))) {
                return true;
            }
        }
        return false;
    }

}
